package util;

import java.util.*;

public class UniqueUtil {

    private static String getUUID() {
        UUID uuid = UUID.randomUUID();
        return uuid.toString();
    }

    public static String getUniqueString() {
        final StringBuilder hex = new StringBuilder(32);
        String uid = getUUID();
        for (int i = 0; i < uid.length(); i++) {
            if (uid.charAt(i) != '-') {
                hex.append(uid.charAt(i));
            }
        }
        return hex.toString();
    }

    public static byte[] getUniqueByte() {
        return (toByte(getUniqueString()));
    }

    public static byte[] toByte(String uid) {
        int i, index, v;
        byte[] b = new byte[uid.length() / 2];
        for (i = 0; i < b.length; i++) {
            index = i * 2;
            v = Integer.parseInt(uid.substring(index, index + 2), 16);
            b[i] = (byte) v;
        }
        return b;
    }

    public static String toString(byte[] uid) {
        final String HEXES = "0123456789ABCDEF";
        final StringBuilder hex = new StringBuilder(2 * uid.length);
        for (final byte b : uid) {
            hex.append(HEXES.charAt((b & 0xF0) >> 4))
                    .append(HEXES.charAt((b & 0x0F)));
        }
        return hex.toString();
    }
}
